package com.fyt.algorithm.Leecode.day8_5;

import java.util.HashMap;
import java.util.Map;

public class SubarraySum {

    public static int subarraySum(int[] nums, int k){
        int res=0;
        Map<Integer,Integer> map=new HashMap<>();
        //初始化第一个数字=k的情况
        map.put(0,1);
        int prefix = 0;
        for (int i = 0; i <nums.length ; i++) {
            prefix+=nums[i];
            if(map.containsKey(prefix-k)){
                res+=map.get(prefix-k);
            }

            map.put(prefix,map.getOrDefault(prefix,0)+1);
        }


        return res;
    }

    public static void main(String[] args) {
        int []nums = {1,2,3};
        int k = 3;

        System.out.println(subarraySum(nums,k));
    }
}
